var isHtml5 = window.applicationCache ? true: false;
var currURL = window.location.origin;
var globalObj = {
	MsgObj : window.top
	
};

!function(){
		window.onerror = function(message, url, line, col, errorObj) {
			MsgBox("文件: " + url.substr(window.location.origin.length) + " \r行: " + line + "   列:" + col + "\r\n错误信息：" + errorObj.message).show();
			return true
		}
// Object.prototype.extend = function(parent) {
// var i;
// this.super = parent;
// for (i in parent) {
// if(this[i]){
// continue;
// }
// if (parent[i].isBaseType() == false) {
// this.i = extendDeep(parent[i]);
// } else {
// this[i] = parent[i];
// }
// }
// return this;
// }
//
//Object.prototype.isBaseType = function() {
//return inheritClass(this,[String,Boolean,Number,Date]);
//}
	loadFile("js/json.js");
	ajaxListen({
		open : onAjaxOpen,
		send : onAjaxSend
	});
}(this);

function inheritClass(obj,...cls) {
for ( var c in cls) {
	if(obj instanceof cls[c]){
		return true;
	}
}
return false;
}
function onAjaxOpen(method, url) {
	console.log(method +": "+ url);
}
function onAjaxSend(method, url, data) {
	console.log({"method":method ,"url": url ,"url": data});
}
/**
 * 向后台请求数据
 * 
 * @param rul
 * @param param
 * @param async
 */
function execRequest(rul, param, _async, type) {
	var rData = {};
	param.r = Math.random();
	$.ajax({
		type : type || "GET",
		url : rul,
		async : _async || false,
		dataType : "json",
		contentType : 'application/json',
		data : type == "GET" ? param : JSON.stringify(param),
		success : function(response) {
			rData = response;
		},
		error : function(e) {
			MsgBox("网络请求异常："+ e.statusText).show();
		}
	});
	return rData;
}
function checkIEVersion() {
    var ua = navigator.userAgent;
    var s = "MSIE";
    var i = ua.indexOf(s);
    if (i >= 0) {
        // 获取IE版本号
        return parseFloat(ua.substr(i + s.length));
    } else {
        return undefined;
    }
}

function isNull(obj){
	switch(obj){
		case null:
		case undefined:
			return true;
		default:
			return false;
	}
}
/**
 * 继承一个JS对象，即复制一个对象。
 * 
 * @param obj
 */
function copyObject(obj) {
    var temObj = {};
    for (var p in obj) {
        if (typeof obj[p] == 'function') {
            temObj[p] = copyObject(obj[p]);
        } else {
            temObj[p] = obj[p];
        }
    }
    return temObj;
}
function overrideProperty(obj,propObj){
	for( var p in propObj){
		obj[p] = propObj[p];
	}
	return obj;
}
function checkAllowPopup() {
    var objChild;
    var reWork = new RegExp('object', 'gi');
    try {
        objChild = window.open('', 'child', 'left=8000,top=8000,width=50,height=50,status=no,resizable=yes');
        objChild.close();
    } catch(e) {}
    if (!reWork.test(String(objChild))) {
        return false;
    } else {
        return true;
    }
}

/**
 * 动态加载js
 * 
 * @param path
 * @param id
 */
function loadJS(path, id, fun) {
    loadFile(path, id, 'script', fun);
}
function loadCss(path, id, fun) {
    loadFile(path, id, "link", fun);
}
function loadFile(path, id, type, fun) {
    if (!path || path.length === 0) {
        throw new Error('加载的文件路径无效。');
    }
    if (document.getElementById(id) != undefined) {
        fun() || fun;
        return;
    }
    var head = document.head || document.getElementsByTagName('head')[0];
    var file = document.createElement(type);
    file.id = id;
    if (type == 'link') {
        file.href = path;
        file.rel = 'stylesheet'
    } else if (type == 'script') {
        file.src = path;
        file.type = 'text/javascript';
    }
    head.insertBefore(file, head.firstChild);
    if (file.readyState) { // IE
        file.onreadystatechange = function() {
            if (file.readyState == "loaded" || file.readyState == "complete") {
            	file.onreadystatechange = null;　
            	fun() || fun;
            }
        };
    } else {
        file.onload = function() {
            fun() || fun;
        };
    }
}

function isObject(obj){
	return type(obj)=="object";
}
function isString(obj) {
	return type(obj)=="string";
}
function isNumber(obj) {
	return type(obj)=="number";
}
function type(obj){
	switch(obj){
		case null:
			return "null";
		case undefined:
			return "undefined";
	}
	var s=Object.prototype.toString.call(obj);
	switch(s){
		case "[object String]":
			return "string";
		case "[object Number]":
			return "number";
		case "[object Boolean]":
			return "boolean";
		case "[object Array]":
			return "array";
		case "[object Date]":
			return "date";
		case "[object Function]":
			return "function";
		case "[object RegExp]":
			return "regExp";
		case "[object Object]":
			return "object";
		default:
			return "object";
	}
}

function triggerEventEle(){
	return document.elementFromPoint(window.event.clientX, window.event.clientY)
}

function log(name,value) {
	console.log(name,value); 
}

function guid() {
    return (S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4());
}
function S4() {
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
 }

	function setMsgScope(scope) {
		globalObj.MsgObj = scope;
	}

	function registerListen(id, cback) {
		if (!globalObj.MsgObj.listen) {
			globalObj.MsgObj.listen = [];
		}
		if (globalObj.MsgObj.listen[id]) {
			globalObj.MsgObj.listen[id].push(cback);
		} else {
			globalObj.MsgObj.listen[id] = [ cback ];
		}
	}

	function delListen(cback) {
		var listen = globalObj.MsgObj.listen[id];
		if (listen) {
			var tmp = [];
			for ( var id in listen) {
				if (id == cback.id) {
					continue;
				}
				if (id == cback.constructor.name) {
					continue;
				}

				tmp[id] = listen[id];
			}
			globalObj.MsgObj.listen[id] = tmp;
		}
	}
	function clearListen(id) {
		globalObj.MsgObj.listen[id] = {};
	}

	function sendMsg(id, obj) {
		var listen = globalObj.MsgObj.listen[id];
		if (listen) {
			for ( var id in listen) {
				try {
					listen[id](obj);
				} catch (e) {
				}
			}
		}
	}

	function getCallPath(arg) {
		var caller = arg ? arg.callee : arguments.callee;
		var path = [];
		while (caller != null) {
			path[path.length] = caller.name;
			caller = caller.caller;
		}
		return path.reverse().join(" -> ");
	}

	function ajaxListen(callback) {
		httpListen(XMLHttpRequest, {
			open : callback.open || function() {
			},
			send : callback.send || function() {
			}
		});
	}
	function httpListen(ajaxObj, callback) {
		if (!ajaxObj || !callback) {
			return;
		}
		var ajaxHandle = {
			tempOpen : ajaxObj.prototype.open,
			tempSend : ajaxObj.prototype.send
		};

		ajaxObj.prototype.open = function(a, b){
			ajaxHandle.method = a;
			ajaxHandle.url = b;
			if (a.toLowerCase() == 'get') {
				ajaxHandle.data = b.split('?')[1];
			} else {
				ajaxHandle.data = null;
			}
			callback.open(ajaxHandle.method, ajaxHandle.url, ajaxHandle.data);
			ajaxHandle.tempOpen.apply(this, arguments);
		}

		ajaxObj.prototype.send = function(a, b) {
			if (ajaxHandle.method.toLowerCase() == 'post') {
				ajaxHandle.data = a;
			} else {
				ajaxHandle.data = null;
			}
			callback.send(ajaxHandle.method, ajaxHandle.url, ajaxHandle.data);
			ajaxHandle.tempSend.apply(this, arguments);
		}
	}